package cn.com.lms.business.exam.persistence.repository;


import cn.com.lms.business.exam.persistence.entity.PaperQuestion;

import com.bnzj.core.persistence.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

@Repository
public interface PaperQuestionRepository extends BaseRepository<PaperQuestion,Long> {

    @Query(value = "select pq.paper_id as paperId,pq.question_id as  questionId,pq.single_count as singleCount,pq.multiple_count as multipleCount,pq.judge_count as judgeCount,pq.fill_count as fillCount,pq.explain_count as explainCount,  eq.question_name as questionName ,eq.is_private as isPrivate,eq.is_delete as isDelete  from lms.bnz_exam_paper_question pq LEFT JOIN lms.bnz_exam_question eq on pq.question_id=eq.id where pq.paper_id in :paperIdList order by pq.paper_id asc ", nativeQuery = true)
    List<Map> findPaperQuestionByPaperIdList(@Param("paperIdList") List<Long> paperIdList);

    @Query(value = "select pq.paper_id  ,sum((pq.single_count*c.single_score) +(pq.multiple_count*c.multiple_score)+(pq.judge_count*c.judge_score)+(pq.fill_count*c.fill_score) +(explain_count*c.explain_score)) paper_score , '607542805b9f42728cf3259110860d82' code_number from bnz_exam_paper_question pq left JOIN bnz_exam_paper_quick_score c on code_number=c.code_number where pq.paper_id in :paperIdList GROUP BY pq.paper_id ", nativeQuery = true)
    List<Map> findPaperScoreByPaperIdList(@Param("paperIdList") List<Long> paperIdList);

    @Transactional
    @Modifying
    @Query(value = "delete from bnz_exam_paper_question where paper_id=?1", nativeQuery = true)
    public void deleteByPaperId(Long paperId);
}

